<?php
	class Order_course_auths extends CI_Model{

	 	function __construct(){
          	parent::__construct();
      	}

        /**
        * 函数功能介绍：返回课程列表（用于分页显示）
        * 创建人:  Yang Ningshu
        * @param id int
        * @return array
        */
        function get_course_list($limit = 0, $offset = 10,$data = 0){
            $where = "data_type = 2 ";
            $order = "v.id desc";

            if(isset($data['school_id']) && $data['school_id'] > 0){
                $where .= " AND v.school_id = '". $data['school_id']."'";
            }

            $sql = "SELECT v.*,cc.school_name,c.title FROM {$this->db->dbprefix}o2o_school_order_records as v left join {$this->db->dbprefix}o2o_school as cc on(v.school_id = cc.id) left join {$this->db->dbprefix}course as c on(v.data_id = c.id)  WHERE $where ORDER BY ".$order." LIMIT $limit, $offset";
            $result = $this->db->query($sql)->result_array();

            $sql = "SELECT count(v.id) AS count FROM {$this->db->dbprefix}o2o_school_order_records as v WHERE $where";
            $count = $this->db->query($sql)->result();
            return array("count" => $count[0]->count, "result" => $result);
        }

        function get_tiku_list($limit = 0, $offset = 10,$data = 0){
            $where = "data_type = 1 ";
            $order = "v.id desc";

            if(isset($data['school_id']) && $data['school_id'] > 0){
                $where .= " AND v.school_id = '". $data['school_id']."'";
            }

            $sql = "SELECT v.*,cc.school_name,s.title FROM {$this->db->dbprefix}o2o_school_order_records as v left join {$this->db->dbprefix}o2o_school as cc on(v.school_id = cc.id) left join {$this->db->dbprefix}b_subject as s on(v.data_id = s.id)  WHERE $where ORDER BY ".$order." LIMIT $limit, $offset";
            $result = $this->db->query($sql)->result_array();

            $sql = "SELECT count(v.id) AS count FROM {$this->db->dbprefix}o2o_school_order_records as v WHERE $where";
            $count = $this->db->query($sql)->result();
            return array("count" => $count[0]->count, "result" => $result);
        }

        function get_shixun_list($limit = 0, $offset = 10,$data = 0){
            $where = "data_type = 5 ";
            $order = "v.id desc";

            if(isset($data['school_id']) && $data['school_id'] > 0){
                $where .= " AND v.school_id = '". $data['school_id']."'";
            }

            $sql = "SELECT SQL_CALC_FOUND_ROWS v.*,cc.school_name FROM {$this->db->dbprefix}o2o_school_order_records as v left join {$this->db->dbprefix}o2o_school as cc on(v.school_id = cc.id)   WHERE $where ORDER BY ".$order." LIMIT $limit, $offset";
            $result = $this->db->query($sql)->result_array();

            $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
            return array("count" => $row_num['row_num'], "result" => $result);
        }


        function get_school(){
            $sql = "SELECT school_name,id from {$this->db->dbprefix}o2o_school where status = 1 and run_status = 0";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }

        function get_order_record_codes($record_id, $data_type){
            if($data_type == 1){
                $sql = "SELECT c.*,u.name FROM {$this->db->dbprefix}o2o_school_system_topic_codes AS c ";
            }elseif($data_type == 2){
                $sql = "SELECT c.*,u.name FROM {$this->db->dbprefix}o2o_school_system_video_course_codes AS c ";
            }elseif($data_type == 5){
                $sql = "SELECT c.*,u.name FROM {$this->db->dbprefix}o2o_school_system_shixun_codes AS c ";
            }
            $sql .= "LEFT JOIN {$this->db->dbprefix}o2o_student AS u ON(u.user_id=c.use_uid) ";
            $sql .= "WHERE c.record_id = $record_id";
            $result = $this->db->query($sql)->result_array();
            return $result; 
        }

        function get_order_record_list($limit= 0, $offset = 0, $conditions = array()){
            $sql = "SELECT SQL_CALC_FOUND_ROWS r.*, school.school_name, subject.title, c.title AS ctitle, a.name AS agent_name, a.points FROM {$this->db->dbprefix}o2o_school_order_records AS r ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}o2o_school AS school ON(school.id = r.school_id) ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}b_subject AS subject ON(subject.id = r.data_id) ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}course AS c ON(c.id = r.data_id) ";
            $sql .= "LEFT JOIN {$this->db->dbprefix}agents AS a ON(a.id = school.agent_id)  ORDER BY r.id DESC ";
            if($limit > 0 || $offset > 0){
                $sql .= "LIMIT $limit, $offset";
            }
            $result = $this->db->query($sql)->result_array();
            if($limit > 0 || $offset > 0){
                $row_num =  $this->db->query('SELECT FOUND_ROWS() as row_num;')->row_array();
                return array("count" => $row_num['row_num'], "result" => $result);
            }
            return $result; 
        }

	}
